package com.web.dao.impl;

import com.web.dao.StudentDao;
import com.web.entity.Student;
import com.web.util.JDBCUtils;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    @Override
    public List<Student> selectStudentAll() {
        List<Student> students =  JDBCUtils.query(Student.class,"select * from student");
        if (students.size() > 0){
            System.out.println(students);
            return  students;
        }
        System.out.println("没有学生信息");
        return null;
    }

    @Override
    public List<Student> selectStudentByName(String name) {
        List<Student> students =  JDBCUtils.query(Student.class,"select * from student where name like ?","%"+name+"%");
        if (students.size() > 0){
            System.out.println(students);
            return  students;
        }
        System.out.println("没有学生信息");
        return null;
    }

    @Override
    public List<Student> selectStudentById(int id) {
        List<Student> students =  JDBCUtils.query(Student.class,"select * from student where id=?",id);
        if (students.size() > 0){
            System.out.println(students);
            return  students;
        }
        System.out.println("没有学生信息");
        return null;
    }

    @Override
    public boolean insertStudent(Student student) {
        int rows = JDBCUtils.update("insert into student(name,age,gender,idcard,address) values(?,?,?,?,?)",
                student.getName(),student.getAge(),student.getGender(),student.getIdcard(),student.getAddress());
         if (rows==1){
             System.out.println("添加成功");
             return true;
         }
         System.out.println("添加失败");
        return false;
    }

    @Override
    public boolean updateStudent(Student student) {
        int rows = JDBCUtils.update("update student set name=?,age=?,gender=?,idcard=?,address=? where id=?",
                student.getName(),student.getAge(),student.getGender(),student.getIdcard(),student.getAddress(),student.getId());
        if (rows==1){
            System.out.println("修改成功");
            return true;
        }
        System.out.println("修改失败");
        return false;
    }

    @Override
    public boolean deleteStudentById(int id) {
        int rows = JDBCUtils.update("delete from student where id=?",id);
        if (rows==1){
            System.out.println("删除成功");
            return true;
        }
        System.out.println("删除失败");
        return false;
    }
}
